package baiduCaptchaV2Resolve

import (
	"context"
	_ "embed"
	"fmt"
	"gitee.com/kaylee595/gkaylee/kerr"
	"os/exec"
	"strings"
)

func GetFS(ctx context.Context, acC float64, initAs, styleBackStr, styleSpin, styleM, styleC string) (string, error) {
	ex := fmt.Sprintf("getFS(%.2f, '%s', '%s', '%s', '%s', '%s')", acC, styleBackStr, initAs, styleSpin, styleM, styleC)
	return execJS(ctx, "./script/baiduCaptchaFS.js", ex)
}

func execJS(ctx context.Context, scriptFile string, ex string) (string, error) {
	output := &strings.Builder{}
	ret := exec.CommandContext(ctx, "node", scriptFile, fmt.Sprintf("console.log(%s)", ex))
	ret.Stdout = output
	ret.Stderr = output
	err := ret.Run()
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	return strings.TrimSpace(output.String()), nil
}
